$(function() {
  //$(document).on('pageInit', '#pageIndex', function() {
  var listContainer = $('#listContainer');
  var dateFilter = $('.date-filter');
  var search = $('#search');
  var loading = false;
  var maxItems = 100;
  var numbPage = 1;

  function addItems(params) {
    var html = '';
    var waringCount = 0;
    var data = $.extend({
      page: 1,
      pageSize: 20,
      type: 'DISPATCH',
      mobile: '13418490922'
    }, params);

    $.ajax({
      url: 'qfang-weixin/qiye/notice/list',
      type: 'POST',
      data: data,
      beforeSend: function() {
        if (!$('.infinite-scroll-preloader').find('.preloader').length) {
          $('.infinite-scroll-preloader').html('<div class="preloader"></div>');
        }

        listContainer.parent().find('.no-data').remove();
      },

      success: function(data) {
        if (data.code === 1000) {
          if (!data.data.items) {
            listContainer.parent().append('<p class="no-data"><span class="iconfont icon-zanwushuju"></span>暂无数据</p>');
            $('.infinite-scroll-preloader').empty();
            return;
          }

          if (data.data.pageCount === 1) {
            $('.infinite-scroll-preloader').empty();
          }

          var html = '';
          $.each(data.data.items, function(k, v) {
            html += '<li>' +
              '<a href="document.html?id=' + v.id + '" class="item-link item-content external">' +
              ' <div class="item-inner">' +
              '   <div class="item-title-row">' +
              '     <div class="item-title">' + v.head + '</div>' +
              '   </div>' +
              '   <div class="row second-content">' +
              '    <div class="col-33">' + v.title + '</div>' +
              '    <div class="col-20">' + v.personName + '</div>' +
              '    <div class="col-33">' + v.date + '</div>' +
              '   </div>' +
              ' </div>' +
              '</a>' +
              '</li>';
          });

          html += '<li>' +
            '<div class="item-inner">' +
            '  <div class="item-title-row">' +
            '   <div class="item-title"></div>' +
            '   </div>' +
            '   <div class="row second-content text-center">总共 ' +
            +data.data.recordCount +
            ' 条</div>' +
            '</div>' +
            '</li>';

          $('#listContainer').append(html);
          maxItems = data.data.recordCount;
        }
      }
    });

  }

  addItems();

  $('#searchBtn').on('click', function() {
    var keyWords = search.val().trim();
    listContainer.empty();
    addItems({
      keyword: keyWords
    });
    keyWords.length && storeHistory(keyWords);
  });

  /* 搜索栏的历史记录 */
  var MaxLength = 10;
  var HistoryKey = 'history' + window.sessionStorage.getItem('u');
  search.on('focus click', function(e) {
    $('#historyList').show();
    e.stopPropagation();
  });

  $('.search-box').on('click', 'li.item', function(e) {
    search.val($(e.target).text()).focus();
    $('#historyList').hide();
    e.stopPropagation();
  });

  $('.search-box').on('click', 'li.clear-history', function(e) {
    window.localStorage.removeItem(HistoryKey);
    $('#historyList').remove();
    e.stopPropagation();
  });

  $('#pageIndex').on('click', function() {
    $('#historyList').hide();
  });

  function storeHistory(item) {
    var list = JSON.parse(window.localStorage.getItem(HistoryKey)) || [];
    var exist = false;

    for (var i = 0, len = list.length >= MaxLength ? MaxLength : list.length; i < len; i++) {
      if (list[i].item === item) {
        list[i].ts = new Date().getTime();
        exist = true;
      }
    }

    if (!exist) {
      list.push({
        item: item,
        ts: new Date().getTime()
      });
    }

    list.sort(function(a, b) {
      return b.ts - a.ts;
    });

    list.splice(MaxLength);

    window.localStorage.setItem(HistoryKey, JSON.stringify(list));
    getHistory();
  }

  function getHistory() {
    var list = JSON.parse(window.localStorage.getItem(HistoryKey));
    $('#historyList').remove();

    if (list && list.length) {
      var html = '<ul id="historyList" class="history-dropdown">';
      for (var i = 0, len = list.length; i < len; i++) {
        html += '<li class="item">' + list[i].item + '</li>';
      }

      html += '<li class="clear-history">清除历史记录</li></ul>';
      $('.search-input').after(html);
    }
  }

  getHistory();

  /* 历史记录结束 */
  // });

  $.init();
});
